package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.common.Result;
import com.example.demo.pojo.Record;
import com.example.demo.pojo.User;
import com.example.demo.service.AdminService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/user/selectAllRecordListOfMine")
    public Result selectAllRecordListOfMine(@RequestParam("Uid") int Uid) {
        try {
            List<Record> recordList= userService.selectAllRecordListOfMine(Uid);
            if (recordList != null || recordList.size() != 0){
                return Result.success(recordList);
            }
            else return Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/user/updateSelf")
    public Result updateSelf(@RequestBody String userStr) {
        try {
            User User = JSON.parseObject(userStr, User.class);
            int i = userService.updateSelf(User);
            return Result.success(i);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/user/insertRecord")
    public Result insertRecord(@RequestBody String recordStr) {
        try {
            Record record = JSON.parseObject(recordStr, Record.class);
            Calendar calendar= Calendar.getInstance();
            Date date=calendar.getTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            record.setRTime(sdf.format(date));
            System.out.println(record);
            int i = userService.insertRecord(record);
            return Result.success(i);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/user/UserLogin")
    public Result UserLogin(@RequestParam("Uid") int Uid) {
        try {
            User user=userService.UserLogin(Uid);
            return Result.success(user);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/user/getUserInfo")
    public Result getUserInfo(@RequestParam("Uid") int Uid) {
        try {
            User user = userService.getUserInfo(Uid);
            return Result.success(user);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }
}
